//弹框和按钮组
import EGlobal from "./EGlobal"
cc.Class({
    extends: cc.Component,

    properties: {
        P_editAlert: {
            default: null,
            type: cc.Node,
            tooltip: "弹框"
        },
        P_input: {
            default: null,
            type: cc.EditBox,
            tooltip: "弹框上的编辑"
        },
        P_title: {
            default: null,
            type: cc.Label,
            tooltip: "弹框上的标题"
        },
        P_ToggleMap: {
            default: null,
            type: cc.Toggle,
            tooltip: "编辑地图"
        },
        P_TogglePath: {
            default: null,
            type: cc.Toggle,
            tooltip: "编辑路径"
        },
        P_ToggleFort: {
            default: null,
            type: cc.Toggle,
            tooltip: "编辑炮台"
        },
        P_editMapButtonGroup: {
            default: null,
            type: cc.Node,
            tooltip: "编辑地图按钮组"
        },
        P_editPathButtonGroup: {
            default: null,
            type: cc.Node,
            tooltip: "编辑路径按钮组"
        }
    },
    onLoad() {
        this.P_editAlert.active = false;
    },
    showAlert(event, data) {
        //显示数组弹框
        if (data == "编辑地图数据") {
            this.P_input.string = ""
            this.setTitle(data)
            this.P_input.string = EGlobal.mapEdit.echoMapData();
            this.P_editAlert.active = true;
        } else if (data == "编辑路径数据") {
            this.P_input.string = ""
            this.setTitle(data)
            this.P_input.string = JSON.stringify(EGlobal.pathList);
            this.P_editAlert.active = true;
        } else if (data == "删除最后一个路径") {
            EGlobal.pathList.pop()
            EGlobal.mapEdit.creatPathBlock()
        } else if (data == "删除所有路径") {
            EGlobal.pathList = []
            EGlobal.mapEdit.creatPathBlock()
        } else if (data == "输出所有数据") {
            this.P_input.string = "";
            this.setTitle(data);
            let mapData = EGlobal.mapEdit.echoMapData();
            let pathData = EGlobal.pathList;
            this.P_input.string = JSON.stringify({
                mapData: JSON.parse(mapData),
                pathData: pathData
            });



        };
        this.P_editAlert.active = true;

    },
    clickOk() {

        if (this.P_title.string == "编辑地图数据") {
            try {
                //生成数组
                let value = JSON.parse(this.P_input.string)
                EGlobal.mapEdit.viewMapItems = value
                EGlobal.mapEdit.creatViewMap()
            } catch (error) {
                console.log(error)
            }
        } else if (this.P_title.string == "编辑路径数据") {
            try {
                //生成数组
                let value = JSON.parse(this.P_input.string)
                EGlobal.pathList = value
                EGlobal.mapEdit.creatPathBlock()
            } catch (error) {
                console.log(error)
            }
        } else if (this.P_title.string == "输出所有数据") {
            try {
                //生成数组
                let value = JSON.parse(this.P_input.string)
                EGlobal.pathList = value.pathData
                EGlobal.mapEdit.viewMapItems = value.mapData
                EGlobal.mapEdit.creatPathBlock();
                EGlobal.mapEdit.creatViewMap();
            } catch (error) {
                console.log(error)
            }
        }
        this.P_editAlert.active = false;
    },
    clickCancel() {
        //点击取消
        this.P_editAlert.active = false;
    },
    setTitle(string) {
        //设置标题
        this.P_title.string = string
    },
    clickToggle(event, data) {
        //选择编辑地图还是路径
        let atllasView = EGlobal.mapEdit.P_editNode.getChildByName("atllasView")
        console.log(data)
        if (data == "编辑地图") {
            this.P_editMapButtonGroup.active = true;
            this.P_editPathButtonGroup.active = false
            atllasView.active = true
        } else if (data == "编辑路径") {
            this.P_editMapButtonGroup.active = false;
            this.P_editPathButtonGroup.active = true
            atllasView.active = false
        } else {
            this.P_editMapButtonGroup.active = false;
            this.P_editPathButtonGroup.active = false;
            
        }
        EGlobal.editType = data
    }

});